﻿using System.Collections;
using System.Collections.Generic;
using Manas.Core.Extensions;
using System.Linq;
using System;
using MKDataBase.Contracts;
namespace MovieModels
{
    public class IMDBMovie
    {
        public string MovieId { get; private set; }
        public string MovieTitle { get; private set; }
        public string ImdbUrl { get; private set; }
        public string Rating { get; private set; }
        public string TagLine { get; private set; }
        public string ReleaseDate { get; private set; }
        public string StoryLine { get; private set; }
        public IEnumerable<string> Genres { get; private set; }
        public string PosterUrl { get; private set; }
        public bool issearched { get; private set; }
        public string searchString { get; set; }
        public IList<BingSearchResult> BingSearchResult { get; set; }
        public int Id { get; set; }
        private IMDBMovie(string searchString)
        {
            this.searchString = searchString;
        }
        public IMDBMovie()
        {

        }
        private IMDBMovie(string moviename,bool extraproperties)
        {
            if (!string.IsNullOrWhiteSpace(moviename))
            {
                IMDBParse movie = new IMDBParse(moviename, extraproperties);
                this.issearched = true;
                MapToCurrentMovieObject(movie);
            }
        }

        private void MapToCurrentMovieObject(IMDBParse movie)
        {
            if (movie.IsNotNull())
            {
               this.MovieId= movie.Id;
                this.ImdbUrl= movie.ImdbURL;
                //movie.Poster;
               this.ReleaseDate= movie.ReleaseDate;
                this.MovieTitle= movie.Title;
                this.TagLine= movie.Tagline;
                this.StoryLine= movie.Storyline;
                // movie.Stars;
                this.Rating= movie.Rating;
                //movie.Plot;
               this.Genres= movie.Genres.ToArray().ToList().Select(s=>Convert.ToString(s));
               this.PosterUrl = movie.Poster;
                //movie.Directors;
                //movie.Cast;
            }
            
        }
        public static IMDBMovie SearchMovie(string movieName, bool extraProperties)
        {
            return new IMDBMovie(movieName, extraProperties);
        }
        public static IMDBMovie GetEmptyMovie(string searchstring)
        {
            return new IMDBMovie(searchstring);
        }
        public IMDBMovie MapDBRowToIMDBMovie(MovieDetailsDataRow row)
        {
            if (row == null)
                return null;

            IMDBMovie movie = new IMDBMovie()
            {
                MovieId = row.IMDBId,
                MovieTitle = row.Title,
                Rating = row.Rating.ToString("0,0"),
                ReleaseDate = row.Releasedate.ToString("dd MMM yyyy"),
                ImdbUrl = row.IMDBUrl,
                TagLine = row.TagLine,
                StoryLine = row.StoryLine,
                Genres = row.Genre.Split('#').Select(s => s)
            };
            return movie;
        }
    }
}
